package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author kabuda
 * @create 2022-09-04 19:44
 */
@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    @Override
    public void saveWithDishDto(DishDto dishDto) {
        //删除全部缓存
        //Set keys = redisTemplate.keys("dish_*");
        //redisTemplate.delete(keys);

        //删除对应分类缓存
        redisTemplate.delete("dish_"+dishDto.getCategoryId());

        //保存菜品
        this.save(dishDto);



        //给菜品口味关联菜品id
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor:flavors){
            dishFlavor.setDishId(dishId);
        }
        //保存菜品口味
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getDishById(Long id) {
        //获得dish并且拷贝到dishdto
        Dish dish = dishService.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //获得CategoryName和Flavors
        Long categoryId = dish.getCategoryId();
        Category category = categoryService.getById(categoryId);
        String categoryName = category.getName();
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(list);
        dishDto.setCategoryName(categoryName);

        return dishDto;
    }

    @Override
    public DishDto editDishByDishDto(DishDto dishDto) {
        //删除全部缓存
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        //删除对应分类缓存
        //redisTemplate.delete("dish_"+dishDto.getCategoryId());
        //更新基本信息
        this.updateById(dishDto);
        //删除菜品对应口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //添加口味
        //给菜品口味关联菜品id
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor:flavors){
            dishFlavor.setDishId(dishId);
        }
        dishFlavorService.saveBatch(flavors);
        return dishDto;
    }

    @Override
    public void removeDish(List<Long> ids) {
        this.removeByIds(ids);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        for (long id :ids){
            queryWrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(queryWrapper);
        }
    }

    @Override
    public void statusChange(int status, List<Long> ids) {
            UpdateWrapper<Dish> updateWrapper = new UpdateWrapper<>();
            for (Long id:ids){
                updateWrapper.eq("id",id);
                //停售改成在售
                updateWrapper.set(status==1,"status",1);
                //在售改成停售
                updateWrapper.set(status==0,"status",0);
                this.update(null,updateWrapper);
            }

    }

    @Override
    public List<DishDto> getDishByCategoryId(Long categoryId) {
        //查询redis是否有数据，如果有直接返回
        String key="dish_"+categoryId;
        List<DishDto> obj = (List<DishDto>) redisTemplate.opsForValue().get(key);
        if(obj!=null){
            return obj;
        }

        //如果没有数据，查询mysql数据库
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,categoryId);
        queryWrapper.eq(Dish::getStatus,1);
        queryWrapper.orderByAsc(Dish::getUpdateTime);
        List<Dish> list = this.list(queryWrapper);

        //创建一个新list
        List<DishDto> dtoList =new ArrayList<>();
        BeanUtils.copyProperties(list,dtoList);
        //遍历records，将
        for (Dish dish : list) {
            Long dishId = dish.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
            List<DishFlavor> flavorList = dishFlavorService.list(lambdaQueryWrapper);

            //创建一个dtoDish对象，将dish拷贝过来，并且设置分类名
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            dishDto.setFlavors(flavorList);

            dtoList.add(dishDto);
        }
        //将数据存储到redis
        redisTemplate.opsForValue().set(key,dtoList,60, TimeUnit.MINUTES);

        return dtoList;
    }


}
